using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class DialogueControllerBubble : MonoBehaviour
{

    // Start is called before the first frame update
    [SerializeField] private Player player;
    [SerializeField]private TextAsset text;
    [SerializeField]private GameObject canvas;
    [SerializeField]private TMP_Text dialogText_A;
    [SerializeField]private TMP_Text dialogText_B;
    [SerializeField]private List<GameObject> panels = new List<GameObject>();
    private Dictionary<string, GameObject> panelDic = new Dictionary<string, GameObject>();
    [SerializeField]private int dialogIndex;

    private string[] rows;
    private bool isTyping;
    private string currentName;
    private bool isFirst = true;

    [SerializeField]private Rigidbody2D rb_balloon;
    [SerializeField] private GameObject windC;

    [SerializeField] private Animator npcAnimator;
    [SerializeField]private SoundManager sm;

    
    private void Awake()
    {
        panelDic["咔咔"] = panels[0];
        panelDic["卖货员"] = panels[1];
        panelDic["守园人"] = panels[1];

    }

    void Start()
    {
        ReadText(text);
        ShowDialogRow();
    }

    // Update is called once per frame
    void Update()
    {
        OnClickContinue();
    }
    private void ReadText(TextAsset textAsset)
    {   
        if (textAsset != null)
        {
            rows = textAsset.text.Split('\n');
        }
    }
    private void UpdateText(string name,string text)
    {
        panelDic[name].SetActive(true);
        currentName = name;

        StartCoroutine(TypeText(text, name));
        
    }
    IEnumerator TypeText(string text, string name)
    {
        if (name == "咔咔")
        {
            isTyping = true;
            string ct = "";
            float typeSpeed =  0.15f;
            foreach (var letter in text)
            {
                ct += letter;
                dialogText_A.text = ct;
                yield return new WaitForSeconds(typeSpeed);
            }

            isTyping = false;
            
            
        }
        else if( name == "卖货员")
        {
            isTyping = true;
            string ct = "";
            float typeSpeed = 0.15f;
            npcAnimator.SetBool("Speak",true);
            foreach (var letter in text)
            {
                ct += letter;
                dialogText_B.text = ct;
                yield return new WaitForSeconds(typeSpeed);
            }

            isTyping = false;
            npcAnimator.SetBool("Speak",false);

        }else if (name == "守园人")
        {
            isTyping = true;
            string ct = "";
            float typeSpeed = 0.15f;
            //npcAnimator.SetBool("Speak",true);
            foreach (var letter in text)
            {
                ct += letter;
                dialogText_B.text = ct;
                yield return new WaitForSeconds(typeSpeed);
            }

            isTyping = false;
            //npcAnimator.SetBool("Speak",false);
        }
    }
    private void ShowDialogRow()
    {
        for (int i = 0; i < rows.Length; i++)
        {
            string row = rows[i];
            string[] cells = row.Split(',');
            if ( cells[0] == "#" &&int.Parse(cells[1]) == dialogIndex)
            {   
                UpdateText(cells[2],cells[3]);
                dialogIndex = int.Parse(cells[4]);
                if(cells[5] != null)
                    Effect(int.Parse(cells[5]));
                break;
            }else if (cells[0] == "%" && int.Parse(cells[1]) == dialogIndex)
            {   player.isInteract = false;
                // nextButton.gameObject.SetActive(false);
                // GenerateOption(i);
            }else if (cells[0] == "END" && int.Parse(cells[1]) == dialogIndex)
            {
                //player.enabled = true;
                isFirst = false;
                canvas.SetActive(false);
                player.isInteract = false;
                if(cells[5] != null)
                    Effect(int.Parse(cells[5]));

            }
        }
        
    }
    public void OnClickContinue()
    {
        if (isFirst)
        {
            if (!isTyping)
            {
                if (Input.GetMouseButton(0))
                {
                    //sm.OnIn(2);
                    panelDic[currentName].SetActive(false);
                    ShowDialogRow();

                }
            }
        }
        else
        {   
            panelDic[currentName].SetActive(true);
            if (Input.GetMouseButton(0))
            {
                //sm.OnIn(2);
                panelDic[currentName].SetActive(false);
                canvas.SetActive(false);
                player.isInteract = false;
            }
        }

    }

    private void Effect(int effectIndex)
    {
        if (effectIndex == 1)
        {
            npcAnimator.SetTrigger("Fetch");

            rb_balloon.gravityScale = -0.01f;
            windC.SetActive(true);
            sm.OnIn(4);
        }else if (effectIndex == 2000) 
        {
            EventHandler.CallUpdateTaskUI(TaskManager.Instance.taskDataList.GetTaskDetail(effectIndex));
        }else if (effectIndex == 2001)
        {
            EventHandler.CallUpdateTaskUI(TaskManager.Instance.taskDataList.GetTaskDetail(effectIndex));
        }
    }

}
